import React, {useState,useEffect} from 'react';
import {inject,observer} from "mobx-react";
import {Table, Modal, Button, Input, Select, Form, message} from 'antd';
import { CloseOutlined,EditOutlined,PlusOutlined} from '@ant-design/icons';
import Axios from "../../util/axios";
import "./Distributiondelivery.css"
function GoodsWarehousing(props) {
    const { Option } = Select;
    const [form] = Form.useForm();
    const [form1] = Form.useForm();
    const columns = [
        {
            title: '行号',
            dataIndex: 'id',
            width: 100,
        },
        {
            title: '状态',
            dataIndex: 'State',
            width: 100,
        },
        {
            title: '单据编号',
            dataIndex: 'Number',
            width: 100,
        },
        {
            title: '商品类型',
            dataIndex: 'CommodityType',
            width: 100,
        },
        {
            title: '供应商',
            dataIndex: 'Supplier',
            width: 100,
        },
        {
            title: '入库门店',
            dataIndex: 'WarehousingStore',
            width: 100,
        },
        {
            title: '来源',
            dataIndex: 'Source',
            width: 100,
        },
        {
            title: '数量',
            dataIndex: 'Quantity',
            width: 100,
        },
        {
            title: '成本',
            dataIndex: 'Cost',
            width: 100,
        },
        {
            title: '操作',
            dataIndex: 'address',
            width: 100,
        }
    ];
    const { Search } = Input;

//表格数据
    const [data,setData]=useState(
        []
    )
    //模态框状态
    const [isModalVisible, setIsModalVisible] = useState(false);
    const [isModalVisible1, setIsModalVisible1] = useState(false);
    //模态框值
    const [modalID,setModalID]=useState('1')

    //点击修改按钮
    const showModal = (obj) => {
        setIsModalVisible(true);
        setModalID(obj.Id)
        form1.setFieldsValue({
            CommodityType:obj.CommodityType,
            CommodityNum:obj.CommodityNum,
            CommodityNmae:obj.CommodityNmae,
            Condit:obj.Condit,
            Weight:obj.Weight,
            Quantity:obj.Quantity,
            Cost:obj.Cost,
        });
    };
    //点击添加按钮
    const showModal1 = () => {
        setIsModalVisible1(true);

    };
    //点击删除
    const delAnniu=(obj)=>{
        Axios.post('/GoodsWarehousing/delMessage',{
            Id:obj.Id
        }).then((res)=>{
                if(res.data.code===200)
                {
                    message.success('删除成功');
                    console.log("删除成功")
                    neironghuoqu()
                }
            }
        )
    }
    const handleOk = () => {
        setIsModalVisible(false);
        setIsModalVisible1(false);
    };
    //确认添加
    const onFinish = (values) => {
        console.log('Success:', values);
        Axios.post('/GoodsWarehousing/addMessage',
            values
        ).then((res)=>{
                if(res.data.code===200)
                {
                    message.success('添加成功');
                    console.log("添加")
                    neironghuoqu()
                }
            }
        )

        form.resetFields();
        setIsModalVisible1(false);
    };
    //确认修改
    const onFinish1 = (values) => {
        console.log('Success:', values);
        Axios.post('/GoodsWarehousing/changeMessage',{
                Id:modalID,
                CommodityType:values.CommodityType,
                CommodityNum:values.CommodityNum,
                CommodityNmae:values.CommodityNmae,
                Condit:values.Condit,
                Weight:values.Weight,
                Quantity:values.Quantity,
                Cost:values.Cost
            }
        ).then((res)=>{
                if(res.data.code===200)
                {
                    message.success('修改成功');
                    console.log("修改")
                    neironghuoqu()
                }
            }
        )

        form1.resetFields();
        setIsModalVisible(false);
    };
    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };

    const handleCancel = () => {
        setIsModalVisible(false);
        setIsModalVisible1(false);
    };

    //获取内容
    const neironghuoqu=()=>{

        Axios.get('/GoodsWarehousing/getMessage',[]).then((res)=>{
            if(res){
                let arr= res.data.data.map((item,key)=>{
                    return {key: key,
                        id:key+1,
                        State:item.State==1?"已通过":(item.State==2?"已反驳":"待审核"),
                        Number:item.Number,
                        CommodityType:item.CommodityType,
                        Supplier:item.Supplier,
                        WarehousingStore:item.WarehousingStore,
                        Source:item.Source,
                        Quantity:item.Quantity,
                        Cost:item.Cost,
                        address:<div>
                            <Button type="primary"  shape="circle" onClick={()=>showModal(item)} >
                                <EditOutlined ></EditOutlined>
                            </Button>
                            <Button type="primary" shape="circle" onClick={()=>delAnniu(item)}>
                                <CloseOutlined></CloseOutlined>
                            </Button>
                        </div>}
                })
                setData(arr)
            }
        })
    }
//搜索
    const onSearch = value => {
        Axios.post('/GoodsWarehousing/findMessage',{Number:value}).then((res)=>{

                if(res){
                    message.success('查询成功');
                    let arr= res.data.data.map((item,key)=>{
                        return {key: key,
                            id:key+1,
                            State:item.State==1?"已通过":(item.State==2?"已反驳":"待审核"),
                            Number:item.Number,
                            CommodityType:item.CommodityType,
                            Supplier:item.Supplier,
                            WarehousingStore:item.WarehousingStore,
                            Source:item.Source,
                            Quantity:item.Quantity,
                            Cost:item.Cost,
                            address:<div>
                                <Button type="primary"  shape="circle" onClick={()=>showModal(item)} >
                                    <EditOutlined ></EditOutlined>
                                </Button>
                                <Button type="primary" shape="circle" onClick={()=>delAnniu(item)}>
                                    <CloseOutlined></CloseOutlined>
                                </Button>
                            </div>}
                    })
                    setData(arr)
                }
            }
        )



    }
    //审核通过
    const adopt=()=>{
        Axios.post('/GoodsWarehousing/passAudit',{Id:modalID}).then((res)=>{
            if(res){
                message.success('审核通过');
                neironghuoqu()
            }
        })
        form1.resetFields();
        setIsModalVisible(false);
    }
//审核驳回
    const reject=()=>{
        Axios.post('/GoodsWarehousing/auditFailed',{Id:modalID}).then((res)=>{
            if(res){
                message.success('审核驳回');
                neironghuoqu()
            }
        })
        form1.resetFields();
        setIsModalVisible(false);
    }

    useEffect(()=>{
            neironghuoqu();
        },[modalID]
    )
    return (
        <div>
            <div><h3>商品入库</h3></div>
            <div className="distInfo">
                <Search placeholder="请输入单据编号" onSearch={onSearch} style={{ width: 200 }} />
                <Button type="primary"  shape="circle" onClick={()=>showModal1()} >
                    <PlusOutlined />
                </Button>
            </div>
            <Table
                rowSelection={{
                    type: 'checkbox'
                }}
                columns={columns}
                dataSource={data}
                pagination={{pageSize: 5}}
                scroll={{y: 240}}/>
            <Modal title="添加" visible={isModalVisible1} footer={
                [] // 设置footer为空，去掉 取消 确定默认按钮
            } onOk={handleOk} onCancel={handleCancel}>
                <Form
                    form={form}
                    name="basic"
                    labelCol={{
                        span: 8,
                    }}
                    wrapperCol={{
                        span: 16,
                    }}
                    initialValues={{
                        remember: true,
                    }}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        label="单据编号"
                        name="Number"

                        rules={[
                            {
                                required: true,
                                message: '请输入单据编号',
                            },
                        ]}
                    >
                        <Input  />
                    </Form.Item>
                    <Form.Item
                        label="商品类型"
                        name="CommodityType"
                        rules={[
                            {
                                required: true,
                                message: '请输入商品类型',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="供应商"
                        name="Supplier"
                        rules={[
                            {
                                required: true,
                                message: '请输入供应商',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="入库门店"
                        name="WarehousingStore"
                        rules={[
                            {
                                required: true,
                                message: '请输入入库门店',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="来源"
                        name="Source"
                        rules={[
                            {
                                required: true,
                                message: '请输入来源',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="商品编号"
                        name="CommodityNum"
                        rules={[
                            {
                                required: true,
                                message: '请输入商品编号',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="商品名字"
                        name="CommodityNmae"
                        rules={[
                            {
                                required: true,
                                message: '请输入商品名字',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="成色"
                        name="Condit"
                        rules={[
                            {
                                required: true,
                                message: '请输入成色',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="重量"
                        name="Weight"
                        rules={[
                            {
                                required: true,
                                message: '请输入重量',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="数量"
                        name="Quantity"
                        rules={[
                            {
                                required: true,
                                message: '请输入数量',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="成本"
                        name="Cost"
                        rules={[
                            {
                                required: true,
                                message: '请输入成本',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        wrapperCol={{
                            offset: 8,
                            span: 16,
                        }}
                    >
                        <Button type="primary" htmlType="submit">
                            提交
                        </Button>
                    </Form.Item>
                </Form>
            </Modal>


            <Modal title="修改" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel} footer={
                [] // 设置footer为空，去掉 取消 确定默认按钮
            }>
                <Form
                    id={'Distributiondel'}
                    form={form1}
                    name="basic"
                    labelCol={{
                        span: 8,
                    }}
                    wrapperCol={{
                        span: 16,
                    }}
                    initialValues={{
                        remember: true,
                    }}
                    onFinish={onFinish1}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        label="商品类型"
                        name="CommodityType"
                        rules={[
                            {
                                required: true,
                                message: '请输入商品类型',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="商品编号"
                        name="CommodityNum"
                        rules={[
                            {
                                required: true,
                                message: '请输入商品编号',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="商品名字"
                        name="CommodityNmae"
                        rules={[
                            {
                                required: true,
                                message: '请输入商品名字',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="成色"
                        name="Condit"
                        rules={[
                            {
                                required: true,
                                message: '请输入成色',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="重量"
                        name="Weight"
                        rules={[
                            {
                                required: true,
                                message: '请输入重量',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="数量"
                        name="Quantity"
                        rules={[
                            {
                                required: true,
                                message: '请输入数量',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="成本"
                        name="Cost"
                        rules={[
                            {
                                required: true,
                                message: '请输入成本',
                            },
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        wrapperCol={{
                            offset: 8,
                            span: 16,
                        }}
                    >
                        <Button type="primary" htmlType="submit">
                            修改
                        </Button>
                        <Button className={props.user.user[0].Juri==3?"Distributiondeldis":"Distributiondelshow"}  type="primary" onClick={()=>adopt()}>
                            审核通过
                        </Button>
                        <Button className={props.user.user[0].Juri==3?"Distributiondeldis":"Distributiondelshow"} type="primary" onClick={()=>reject()}>
                            驳回
                        </Button>
                    </Form.Item>
                </Form>

            </Modal>

        </div>
    );
}

export default inject('user')(observer(GoodsWarehousing));